package cn.tedu.bece.admin.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.tedu.bece.admin.common.exception.ServiceException;
import cn.tedu.bece.admin.system.mapper.SysRoleMapper;
import cn.tedu.bece.admin.system.mapper.SysRoleMenuMapper;
import cn.tedu.bece.admin.system.pojo.dto.SysRoleAddParam;
import cn.tedu.bece.admin.system.pojo.dto.SysRoleQuery;
import cn.tedu.bece.admin.system.pojo.dto.SysRoleUpdateParam;
import cn.tedu.bece.admin.system.pojo.entity.SysRole;
import cn.tedu.bece.admin.system.pojo.entity.SysRoleMenu;
import cn.tedu.bece.admin.system.pojo.vo.SysRoleDetailVO;
import cn.tedu.bece.admin.system.service.ISysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public void insert(SysRoleAddParam sysRoleAddParam) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleAddParam, sysRole);
        // 新增角色信息
        sysRoleMapper.insert(sysRole);
        // 新增角色和菜单关联
        insertRoleMenu(sysRole.getId(), sysRoleAddParam.getMenuIds());
    }

    private void insertRoleMenu(Long roleId, List<Long> menuIds) {
        if (CollectionUtil.isNotEmpty(menuIds)) {
            List<SysRoleMenu> roleMenuList = new ArrayList<>(menuIds.size());
            for (Long menuId : menuIds) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menuId);
                roleMenuList.add(sysRoleMenu);
            }
            sysRoleMenuMapper.insertBatchRoleMenu(roleMenuList);
        }
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        // 删除角色和菜单关联
        sysRoleMenuMapper.deleteRoleMenuByRoleIds(ids);
        // 删除角色信息
        sysRoleMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(SysRoleUpdateParam sysRoleUpdateParam) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleUpdateParam, sysRole);
        // 修改角色信息
        sysRoleMapper.updateById(sysRole);
        // 删除角色和菜单关联
        sysRoleMenuMapper.deleteRoleMenuByRoleId(sysRole.getId());
        // 新增角色和菜单关联
        insertRoleMenu(sysRole.getId(), sysRoleUpdateParam.getMenuIds());
    }

    @Override
    public SysRoleDetailVO selectById(Long id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        if (sysRole != null) {
            SysRoleDetailVO sysRoleDetailVO = new SysRoleDetailVO();
            BeanUtils.copyProperties(sysRole, sysRoleDetailVO);
            sysRoleDetailVO.setMenuIds(sysRoleMenuMapper.selectMenuIdsByRoleId(id));
            return sysRoleDetailVO;
        }
        return null;
    }

    @Override
    public List<SysRole> selectList(SysRoleQuery query) {
        return sysRoleMapper.selectSysRoleList(query);
    }

}
